package com.ztk.threadbasic.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import org.apache.poi.ss.usermodel.DateUtil;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 获取第一行的信息
 */
public class EasyExcelDemo4 {

    private static String STRING_TYPE = "STRING";

    private static String NUMBER_TYPE = "NUMBER";
    public static void main(String[] args) {
        CompanyVO companyVO = new CompanyVO();

        EasyExcel.read("D:\\预算监测表V4.xlsx", new ReadListener<Map<Object, Object>>() {
            @Override
            public void invokeHead(Map headMap, AnalysisContext context) {
                Collection values = headMap.values();
                Iterator iterator = values.iterator();
                while(iterator.hasNext()) {
                    ReadCellData readCellData = (ReadCellData) iterator.next();
                    Integer columnIndex = readCellData.getColumnIndex();
                    CellDataTypeEnum typeEnum = readCellData.getType();
                    String typeEnumName = typeEnum.name();
                    String stringValue = null;
                    if (columnIndex == 1 || columnIndex == 3) {
                        if (STRING_TYPE.equalsIgnoreCase(typeEnumName)) {
                            stringValue = readCellData.getStringValue();
                            companyVO.setBudgetAgency(stringValue);
                        }
                        if (NUMBER_TYPE.equalsIgnoreCase(typeEnumName)) {
                            BigDecimal numberValue = readCellData.getNumberValue();
                            Date javaDate = DateUtil.getJavaDate(numberValue.doubleValue());
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                            stringValue = simpleDateFormat.format(javaDate);
                            companyVO.setBudgetDate(stringValue);
                        }
                    }
                    System.out.println(stringValue);
                }
                System.out.println(companyVO.getBudgetAgency() + "-" + companyVO.getBudgetDate());
            }

            @Override
            public void invoke(Map<Object, Object> map, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("读取完成");
            }
        }).sheet().doRead();

//        dataList.forEach(System.out::println);

    }
}

//class CustomEventListener extends AnalysisEventListener<CompanyVO> {
//    private int currentRow = 0;
//
//    private String
//
//    @Override
//    public void invoke(CompanyVO companyVO, AnalysisContext analysisContext) {
//
//    }
//
//    @Override
//    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//
//    }
//}
